<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2021.3.23
 * Time: 17:23
 */

class Solution
{
    public function moveZeroes($array)
    {
        $arrayLength = count($array);
        if($arrayLength == 0) {
            return false;
        }

        $newArr = [];
        $zeroCount = 0;
        for ($i=0;$i<$arrayLength;$i++) {
            if($array[$i] == 0) {
                ++$zeroCount;
            } else {
                $newArr[] = $array[$i];
            }
        }

        while ($zeroCount > 0) {
            array_push($newArr,0);
            $zeroCount--;
        }
        return $newArr;
    }

    public function moveZeros1($array)
    {
        $arrayLength = count($array);
        if($arrayLength == 0) {
            return false;
        }

        $newArr = [];
        $zeroCount = 0;
        for ($i=0;$i<$arrayLength;$i++) {
            if($array[$i] == 0) {
                ++$zeroCount;
            } else {
                $newArr[] = $array[$i];
            }
        }

        while ($zeroCount > 0) {
            array_push($newArr,0);
            $zeroCount--;
        }
        return $newArr;
    }

    function moveZeroes1(&$array)
    {
        $arrayLength = count($array);
        if($arrayLength == 0) {
            return false;
        }

        $left = 0; // 设置初始左边的值
        $right = 0; // 设置初始右边的值
        // 左边以左的，不是0
        // 左边到右边中间的，是 0
        while ($right < $arrayLength) {
            if($array[$right] != 0) {
                if($right != $left) {
                    $this->swap($array,$left,$right);
                }
                // 是要将不是0 的数移到左边
                $left++;
            }
            $right++;
        }
    }

    public function swap(&$array,$left,$right)
    {
        $temp = $array[$left];
        $array[$left] = $array[$right];
        $array[$right] = $temp;
    }


}

$array = [0,1,0,3,0,12];
$solution = new Solution();
$res = $solution->moveZeroes1($array);
var_dump($array);